package com.kanche.bigbaby.collection;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.ViewDragHelper;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.domain.VehicleModel;
//import com.kkche.merchant.domain.VehicleModel;
//import com.kkche.merchant.fragments.VehicleModelSelectorFragment;

/**
 * Created by nealmi on 6/12/14.
 */
public class VehicleModelSelectorActivity extends BaseActivity implements VehicleModelSelectorFragment.ItemSelectedListener {


    private VehicleModelSelectorFragment seriesFragment;
    private VehicleModelSelectorFragment modelFragment;
    private boolean allowUnlimited = false;
    private Intent currentData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vehicle_selector);
        toolbar = justFindIt(R.id.toolbar);
        toolbar.setTitle("必填信息");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        allowUnlimited = getIntent().getBooleanExtra("allowUnlimited", false);

        VehicleModelSelectorFragment brandsFragment = new VehicleModelSelectorFragment();
        brandsFragment.setType(VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND);
        Bundle args = new Bundle();
        args.putBoolean("allowUnlimited", allowUnlimited);
        brandsFragment.setArguments(args);
        brandsFragment.setItemSelectedListener(this);

        getFragmentManager().beginTransaction()
                .add(R.id.content_frame, brandsFragment, "brandSelector")
                .commit();

        if (allowUnlimited) {
            Button doneButton = (Button) findViewById(R.id.action_done);
            doneButton.setVisibility(View.VISIBLE);
            doneButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentData != null) {
                        setResult(RESULT_OK, currentData);

                        finish();
                        overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
                    } else {
                        finish();
                    }
                }
            });
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                setResult(RESULT_CANCELED);
                finish();
                overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(int type, VehicleModel value) {
        value.cleanUnlimitedLabels();
        currentData = new Intent();
        currentData.putExtra(KKCheDBHelper.VehicleColumns.levelId, "");
        currentData.putExtra("label", value.getDesc());
        currentData.putExtra(KKCheDBHelper.VehicleColumns.brand, value.getBrand());
        currentData.putExtra(KKCheDBHelper.VehicleColumns.saleName, value.getSaleName());
        currentData.putExtra(KKCheDBHelper.VehicleColumns.series, value.getSeries());

        if (allowUnlimited && value.getId() == VehicleModel.UNLIMITED_ID) {
            setResult(RESULT_OK, currentData);
            finish();
            overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
            return;
        }
        if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND) {

            if (seriesFragment == null) {
                seriesFragment = new VehicleModelSelectorFragment();
                seriesFragment.setType(VehicleModelSelectorFragment.VEHICLE_TYPE_SERIES);
                seriesFragment.setQueryModel(value);
                Bundle args = new Bundle();
                args.putBoolean("allowUnlimited", allowUnlimited);
                seriesFragment.setArguments(args);
                seriesFragment.setItemSelectedListener(this);

                getFragmentManager().beginTransaction()
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
//                        .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right)
                        .add(R.id.content_frame, seriesFragment, "seriesSelector")
                        .commit();
            }
            if (!seriesFragment.isVisible()) {
                seriesFragment.show();
            }
            seriesFragment.updateDataWithCondition(value);

            if (modelFragment != null && modelFragment.isVisible())
                modelFragment.hide();
//                getFragmentManager().beginTransaction()
//                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE)
////                        .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right)
//                        .hide(modelFragment)
////                        .detach(modelFragment)
//                        .commit();

        }
        if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_SERIES) {
            if (modelFragment == null) {
                modelFragment = new VehicleModelSelectorFragment();
                modelFragment.setQueryModel(value);
                modelFragment.setType(VehicleModelSelectorFragment.VEHICLE_TYPE_MODEL);
                modelFragment.setItemSelectedListener(this);
                Bundle args = new Bundle();
                args.putBoolean("allowUnlimited", allowUnlimited);
                modelFragment.setArguments(args);
                getFragmentManager().beginTransaction()
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
//                        .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right)
                        .add(R.id.content_frame, modelFragment, "modelSelector")
                        .commit();
            } else {
                if (!modelFragment.isVisible())
                    modelFragment.show();
//                    getFragmentManager().beginTransaction()
//                            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
////                            .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right)
//                            .show(modelFragment)
////                            .attach(modelFragment)
//                            .commit();
                modelFragment.updateDataWithCondition(value);
            }
        }
        if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_MODEL) {
            Intent data = new Intent();
            data.putExtra(KKCheDBHelper.VehicleColumns.levelId, value.getLevelId());
            data.putExtra("label", value.getDesc());
            data.putExtra(KKCheDBHelper.VehicleColumns.brand, value.getBrand());
            data.putExtra(KKCheDBHelper.VehicleColumns.yearGroup, value.getYearGroup());
            data.putExtra(KKCheDBHelper.VehicleColumns.saleName, value.getSaleName());
            data.putExtra(KKCheDBHelper.VehicleColumns.series, value.getSeries());
            setResult(RESULT_OK, data);

            finish();
            overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);

        }
    }

    private class DragHelperCallback extends ViewDragHelper.Callback {
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return false;
        }
    }
}
